<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="报表名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reportId">
              <a-select v-model="model.reportId" placeholder="请选择报表名称"
                        @change="handleChange">
                <a-select-option
                  :key="item.tablecode"
                  v-for="item in toptions"
                  :value="item.tablecode"
                >{{item.tablename}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="指标名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tname">
              <a-select v-model="model.tname" placeholder="请选择指标名称" @change="handleNChange">
                <a-select-option
                  :key="item.columncode"
                  v-for="item in coptions"
                  :value="item.columnvalue"
                >{{item.columnvalue}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="指标编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tcode">
              <a-input v-model="model.tcode" placeholder="请输入指标编码"  :readOnly="true"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="阈值类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ttype">
              <j-dict-select-tag v-model="model.ttype" placeholder="请选择阈值类型" dictCode="LLBB_PARAM_CONFIG,NAME,CODE,pid='1234194297089617901'"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上限值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="uplimit">
              <a-input-number v-model="model.uplimit" placeholder="请输入上限值" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="下限值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="downlimit">
              <a-input-number v-model="model.downlimit" placeholder="请输入下限值" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {initDictOptions, filterDictText} from '@/components/dict/JDictSelectUtil'
  export default {
    name: 'JrsjThresholdValueForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
        },
        toptions:[],
        coptions:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/system/jrsjThresholdValue/add",
          edit: "/system/jrsjThresholdValue/edit",
          queryById: "/system/jrsjThresholdValue/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initDictConfig();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      handleChange(){
        var rid=this.model.reportId;
        var param=new Object();
        var that=this;
        getAction("/pDataConfig/jsPDataConfig/slist?tablename="+rid,param).then(res=> {
          that.coptions=res.result;
        });
      },
      handleNChange(e1,e2){
        this.model.tcode=e2.key;
      },
      initDictConfig() {
        var param=new Object();
        var that=this;
        getAction("/pDataConfig/jsPDataConfig/blist",param).then(res=> {
          that.toptions=res.result;
        });
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if(this.model.id) {
          var that=this;
          getAction("/pDataConfig/jsPDataConfig/slist?tablename=" + this.model.reportId).then(res => {
            that.coptions = res.result;
          });
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>